package com.sunda.spmsoversea.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.entity.OverseaInvCheckActionDtl;
import com.sunda.spmsoversea.mapper.OverseaInvCheckActionDtlMapper;
import com.sunda.spmsoversea.service.IOverseaInvCheckActionDtlService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 盘盈盘亏明细表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-05-10
 */
@Service
public class OverseaInvCheckActionDtlServiceImpl extends ServiceImpl<OverseaInvCheckActionDtlMapper, OverseaInvCheckActionDtl> implements IOverseaInvCheckActionDtlService {
    /*获取InventoryCheckAction行项*/
    @Override
    public ResponseResult getInvCheckAcDtl(String uuid) {
        if (StringUtils.isEmpty(uuid)) {
            return ResponseResult.error("uuid is mandatory required");
        }
        try {
            return ResponseResult.success().add("invCheckAcDtls", this.baseMapper.selectInvDtl(uuid));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("failed when query Inventory Check Action Dtl");
        }
    }

    /*更新InventoryCheckAcDtl行项
     * 以uuid及taskRowId为key，存在则更新，不存在则新建
     * */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult updateInvCheckDtl(String uuid, JSONArray invCheckAcDtls) {
        try {
            if (StringUtils.isEmpty(uuid)) {
                throw new Exception("uuid is mandatory required");
            }

            for (int i = 0; i < invCheckAcDtls.size(); i++) {
                OverseaInvCheckActionDtl inventoryCheckActionDtl = invCheckAcDtls.getObject(i, OverseaInvCheckActionDtl.class);
                QueryWrapper<OverseaInvCheckActionDtl> invAcDtlQuerywarp = new QueryWrapper<OverseaInvCheckActionDtl>().eq("UUID", uuid).eq("TASK_ROW_ID", inventoryCheckActionDtl.getTaskRowId());
                List<OverseaInvCheckActionDtl> invAcDtls = this.baseMapper.selectList(invAcDtlQuerywarp);
                if (invAcDtls.size() > 0) {
                    // 存在则更新
                    this.baseMapper.update(inventoryCheckActionDtl, invAcDtlQuerywarp);
                } else {
                    // 不存在，新建
                    if (StringUtils.isEmpty(inventoryCheckActionDtl.getTaskRowId())) {
                        int taskRowId = this.baseMapper.selectMaxTaskRowId(uuid);
                        taskRowId += 10;
                        inventoryCheckActionDtl.setTaskRowId(String.valueOf(taskRowId));
                    }
                    inventoryCheckActionDtl.setUuid(uuid);
                    this.baseMapper.insert(inventoryCheckActionDtl);
                }
            }
            return ResponseResult.success().add("invCheckAcDtls", this.baseMapper.selectList(new QueryWrapper<OverseaInvCheckActionDtl>().eq("UUID", uuid)));
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseResult.error(e.getMessage());
        }
    }

    @Override
    public List<OverseaInvCheckActionDtl> getICADList(String uuid) {
        return this.baseMapper.selectList(new QueryWrapper<OverseaInvCheckActionDtl>().eq("UUID", uuid));
    }

    @Override
    public List<Map<String, Object>> getInventoryActionDtl(String uuid) {
        return this.baseMapper.getInventoryActionDtl(uuid);
    }

     /**获取带物料信息的行项,用于提交OA */
    @Override
    public List<Map<String, Object>> getOverseaInvChkDtls(String uuid) {
        return this.baseMapper.selectInvDtl(uuid);
    }

}
